package com.tavio.tbs.activities;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.tmx.util.exception.TMXLoadException;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

import com.tavio.tbs.scene.SceneManager;
import com.tavio.tbs.sprite.UnitSprite;
import com.tavio.tbs.structure.Position;
import com.tavio.tbs.unit.Soldier;
import com.tavio.tbs.unit.Unit;
import com.tavio.tbs.world.WorldMap;

public class TesteActivity extends SimpleBaseGameActivity {
    
	public static final int CAMERA_WIDTH = 800;
	public static final int CAMERA_HEIGHT = 480;
	
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TiledTextureRegion mSoldierTextureRegion;
	
	public EngineOptions onCreateEngineOptions() {
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

		return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}
	@Override
	protected void onCreateResources() {
		
		try {
		
		WorldMap.getInstance().init("tmx/map.tmx", this.getAssets(), this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, this.getVertexBufferObjectManager());
		SceneManager.getInstance().init(this.getVertexBufferObjectManager(), this.getTextureManager(), this.getAssets());
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 128, 128);
		this.mSoldierTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this.getAssets(), "soldier.png", 0, 0, 3, 1);
		this.mBitmapTextureAtlas.load();

			
		} catch (TMXLoadException e) {
			Debug.e(e);
		}

	}
	
	@Override
	protected Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		UnitSprite soldierSprite = new UnitSprite(0, 0, 30, 40, this.mSoldierTextureRegion, this.getVertexBufferObjectManager());
		
		Unit soldier = new Soldier(new Position(0,0), 2, soldierSprite);
		
		soldierSprite.setUnit(soldier);
		
		soldierSprite.setZIndex(2);
		
		WorldMap.getInstance().getTmxLayer(0).setZIndex(0);
			
		SceneManager.getInstance().getScene().registerTouchArea(soldierSprite);
		
		soldier.move(new Position(13,9));
		
		SceneManager.getInstance().getScene().attachChild(soldier.getSprite()); 
		
		SceneManager.getInstance().getScene().attachChild(WorldMap.getInstance().getTmxLayer(0));
		
		SceneManager.getInstance().getScene().sortChildren();
		
		return SceneManager.getInstance().getScene();
	}
	
}
